package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.FriendNews;
import cn.wolfcode.trip.base.domain.FriendNewsComment;
import cn.wolfcode.trip.base.domain.FriendNewsLiker;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IFriendNewsService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;

@RestController
@RequestMapping("/friendNews")
public class FriendNewsController {

    @Autowired
    private IFriendNewsService friendNewsService;
    @Autowired
    private IUserService userService;

    @PostMapping
    public Object save(FriendNews friendNews,Long[] aitedfriendIds, Long[] pointedfriendIds){
        friendNews.setFriend(UserContext.getCurrentUser());
        friendNewsService.save(friendNews,aitedfriendIds,pointedfriendIds);
        return new JSONResult();
    }

    @PostMapping("/{friendNews_id}/liker")
    @ResponseBody
    public Object likerForFriendNews(FriendNewsLiker friendNewsLiker){
        Long currentUserId = UserContext.getCurrentUser().getId();
        friendNewsLiker.setLiker_id(currentUserId);
        friendNewsService.likerForFriendNews(friendNewsLiker);
        return new JSONResult();
    }
    @PostMapping("/{id}/reply/{byUserId}")
    @ResponseBody
    public Object commentForFriendNews(@PathVariable Long id,@PathVariable Long byUserId, FriendNewsComment friendNewsComment){
        JSONResult result = new JSONResult();
        HashMap map = new HashMap();
        User currentUser = UserContext.getCurrentUser();
        friendNewsComment.setReply(currentUser);
        FriendNews friendNews = new FriendNews();
        friendNews.setId(id);
        friendNewsComment.setFriendNews(friendNews);
        //查询动态发表人id
        Long friendNewsPublishPeopleId=friendNewsService.selectFriendNewsPublishPeopleIdByFriendNewsId(id);
        map.put("dynamicId",id);
        map.put("user",currentUser);
        map.put("content",friendNewsComment.getContent());
        if(byUserId==-1){
            User byUser = new User();
            byUser.setId(-1L);
            friendNewsComment.setByUser(byUser);
            friendNewsService.commentForFriendNews(friendNewsComment);
            result.setData(map);
        }else {
            //查询被回复人
            User byUser=userService.get(byUserId);
            map.put("byUser",byUser);
            friendNewsComment.setByUser(byUser);
            friendNewsService.commentForFriendNews(friendNewsComment);
            result.setData(map);
        }
        return result;

    }
}
